package com.ozo.cloud.gen.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ozo.cloud.common.base.model.BaseBatchDTO;
import com.ozo.cloud.common.base.model.BasePage;
import com.ozo.cloud.common.base.model.PageResult;
import com.ozo.cloud.common.base.model.ResultVO;
import com.ozo.cloud.common.log.annotation.OperateLog;
import com.ozo.cloud.gen.pojo.dto.*;
import com.ozo.cloud.gen.pojo.vo.GenTableVO;
import com.ozo.cloud.gen.service.IGenTableService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 表配置 前端控制器
 * </p>
 *
 * @author qiangesoft
 * @since 2024-11-12
 */
@Tag(name = "表配置管理")
@RequiredArgsConstructor
@RestController
@RequestMapping("/table")
public class GenTableController {

    private final IGenTableService genTableService;

    @OperateLog(value = "导入表", module = "代码生成表")
    @SaCheckPermission("gen:table:import")
    @Operation(summary = "导入表")
    @PostMapping("/import")
    public ResultVO<Void> importTable(@RequestBody @Valid GenTableImportDTO tableImportDTO) {
        genTableService.importTable(tableImportDTO);
        return ResultVO.success();
    }

    @OperateLog(value = "自动建表", module = "代码生成表")
    @SaCheckPermission("gen:table:create")
    @Operation(summary = "自动建表")
    @PostMapping("/create")
    public ResultVO<Void> createTable(@RequestBody @Valid GenTableCreateDTO tableCreateDTO) {
        genTableService.createTable(tableCreateDTO);
        return ResultVO.success();
    }

    @OperateLog(value = "删除表", module = "代码生成表")
    @SaCheckPermission("gen:table:remove")
    @Operation(summary = "删除表")
    @DeleteMapping("/remove")
    public ResultVO<Void> remove(@RequestBody @Valid BaseBatchDTO<Long> batchDTO) {
        genTableService.remove(batchDTO);
        return ResultVO.success();
    }

    @OperateLog(value = "修改配置", module = "代码生成表")
    @SaCheckPermission("gen:table:edit")
    @Operation(summary = "修改配置")
    @PutMapping("/edit")
    public ResultVO<Void> edit(@RequestBody @Valid GenTableDetailDTO saveDTO) {
        genTableService.edit(saveDTO);
        return ResultVO.success();
    }

    @SaCheckPermission("gen:table:page")
    @Operation(summary = "表分页")
    @GetMapping("/page")
    public ResultVO<PageResult<GenTableVO>> page(@Valid BasePage basePage, GenTableQueryDTO queryDTO) {
        return ResultVO.success(genTableService.page(basePage, queryDTO));
    }

    @Operation(summary = "表详情")
    @GetMapping("/{id}")
    public ResultVO<GenTableDetailDTO> detail(@PathVariable Long id) {
        return ResultVO.success(genTableService.detail(id));
    }

    @OperateLog(value = "代码生成", module = "代码生成表")
    @SaCheckPermission("gen:table:genLocal")
    @Operation(summary = "代码生成")
    @PostMapping("/genLocal/{id}")
    public ResultVO<Void> genLocal(@PathVariable Long id) {
        genTableService.generateLocal(id);
        return ResultVO.success();
    }

    @OperateLog(value = "代码下载", module = "代码生成表")
    @SaCheckPermission("gen:table:genDownload")
    @Operation(summary = "代码下载")
    @GetMapping("/genDownload/{id}")
    public void genDownload(@PathVariable Long id, HttpServletResponse response) {
        genTableService.generateDownload(id, response);
    }

    @Operation(summary = "数据库表分页")
    @GetMapping("/pageDbTable")
    public ResultVO<PageResult<GenTableVO>> pageDbTable(@Valid BasePage basePage, GenTableQueryDTO queryDTO) {
        return ResultVO.success(genTableService.pageDbTable(basePage, queryDTO));
    }

}
